package com.norman.sorry.core.tiles;

import java.awt.Color;

/**
 * A final utility class that provides various methods dealing with tiles.
 * 
 * @author Brian Norman
 * @version 0.0.1 beta
 * @since 0.0.1 beta
 */
public final class TileUtils {

   /**
    * Private constructor so no one can create an instance of this class.
    */
   private TileUtils() {
   }

   /**
    * Returns the tile that is <code>repeat</code> tiles away. If
    * <code>repeat</code> is greater than zero, returns the tile that is that
    * far away forward. If it is less than zero, returns the tile that is that
    * far away backwards.
    * 
    * @param start
    *           the starting linked tile.
    * @param color
    *           the color of the piece that is moving.
    * @param repeat
    *           how far to move.
    * @return the tile that is <code>repeat</code> tiles away.
    */
   public static final ILinkedTile get(ILinkedTile start, Color color, int repeat) {
      if (repeat < 0) {
         return getPrevious(start, -repeat);
      } else {
         return getNext(start, color, repeat);
      }
   }

   /**
    * Returns the tile that is <code>repeat</code> tiles away in the forward
    * direction. If <code>repeat</code> is less than or equal to zero, the
    * starting tile is returned.
    * 
    * @param start
    *           the starting linked tile.
    * @param color
    *           the color of the piece that is moving.
    * @param repeat
    *           how far to move in the forward direction.
    * @return the tile in the forward direction.
    */
   public static final ILinkedTile getNext(ILinkedTile start, Color color, int repeat) {
      for (int i = 0; i < repeat && start != null; i++) {
         start = start.getNext(color);
      }
      return start;
   }


   /**
    * Returns the tile that is <code>repeat</code> tiles away in the backward
    * direction. If <code>repeat</code> is less than or equal to zero, the
    * starting tile is returned.
    * 
    * @param start
    *           the starting linked tile.
    * @param repeat
    *           how far to move in the backward direction.
    * @return the tile in the backward direction.
    */
   public static final ILinkedTile getPrevious(ILinkedTile start, int repeat) {
      for (int i = 0; i < repeat && start != null; i++) {
         start = start.getPrevious();
      }
      return start;
   }

}
